package org.sys.transporttrainsys.service;

import org.sys.transporttrainsys.entity.Organization;
import org.sys.transporttrainsys.repository.OrganizationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrganizationService {

    @Autowired
    private OrganizationRepository organizationRepository;

    public List<Organization> getAllOrganizations() {
        return organizationRepository.findAll();
    }

    public Organization getOrganizationById(Long id) {
        return organizationRepository.findById(id).orElse(null);
    }

    public Organization createOrganization(Organization organization) {
        return organizationRepository.save(organization);
    }

    public Organization updateOrganization(Long id, Organization organizationDetails) {
        Organization organization = organizationRepository.findById(id).orElse(null);
        if (organization != null) {
            organization.setName(organizationDetails.getName());
            organization.setType(organizationDetails.getType());
            // 更新其他字段，如果需要
            return organizationRepository.save(organization);
        }
        return null;
    }

    public void deleteOrganization(Long id) {
        organizationRepository.deleteById(id);
    }

    // 根据名称查询机构
    public List<Organization> findOrganizationsByName(String name) {
        return organizationRepository.findByName(name);
    }

    // 根据类型查询机构
    public List<Organization> findOrganizationsByType(String type) {
        return organizationRepository.findByType(type);
    }

    // 根据名称和类型联合查询机构
    public List<Organization> findOrganizationsByNameAndType(String name, String type) {
        return organizationRepository.findByNameAndType(name, type);
    }
}